<?php
namespace Admin\Controller;
use Think\Controller;
//脑筋急转弯
class YaoController extends CommonController {

    public function index(){
        self::user_list();
    }

    //用户模块
    public function user_list()
    {





        $count      = M('yao_member')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $data['user_list'] = M('yao_member')->order('yao_member_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);// 赋值分页输出

        $this->assign("data",$data);

        $this->display('user_list');
    }

    //好友
    public function frend_ranking(){

        $wxuserid = I('get.wxusers_id','','int');
        $where['uid'] = $wxuserid;

        if (empty($wxuserid))
        {
            $this->error('非法请求');
        }

        $coupling_uids = M('WxusersRelationship')->where($where)->getField('coupling_uid',true);

        if (empty($coupling_uids)){
            $coupling_uids = array();
        }

        $coupling_uids[] = $wxuserid;

        $uids = array();
        $uids = M('WxusersRelationship')->where(array('coupling_uid'=>$wxuserid))->getField('uid',true);
        if (empty($uids)){
            $uids = array();
        }

        $ids = array_unique(array_merge($coupling_uids,$uids));//合并去重

        $where1['wxusers_id'] = array('in',$ids);
        $user = "";
        if ($ids){
            $user = M('Wxusers')->field('wxusers_id,nickName,avatarUrl,checkpoint_final')->where($where1)->limit(100)->order('checkpoint_final desc')->select();
        }

        $this->assign('data',$user);
        $this->display('frend_ranking');

    }

    //更多好玩
    public function game_more()
    {
        $data['game_list']  =   M('appshow')->select();
        $this->assign("data",$data);
        $this->display('game_more');
    }

    public function game_more_add()
    {
        if (IS_AJAX)
        {
            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('appshow')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_more_add');
        }
    }

    public function game_more_edit()
    {
        if (IS_AJAX)
        {
            $where['appshow_id']  =   I('post.appshow_id','','htmlspecialchars');
            if (empty($where['appshow_id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('appshow')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['appshow_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['appshow_id']))
            {
                $this->error('非法请求');
            }

            $field      =   array('appshow_id','appid','url','envVersion','logo','content','isshow','title');
            $data['game_info']  =   M('appshow')->where($where)->field($field)->find();

            if ( empty($data))
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_more_edit');
        }
    }

    //游戏题库
    public function game_question()
    {
        $field                  =   array('quersion_id','answer','img_src','confusingdata','sort','createtime','updatetime','appid');
        $data['game_question']  =   M('quersion')->order('quersion_id desc')->field($field)->select();
        $this->assign("data",$data);
        $this->display('game_question');
    }

    public function game_question_edit()
    {
        if (IS_AJAX)
        {
            $where['quersion_id']  =   I('post.quersion_id','','htmlspecialchars');
            if (empty($where['quersion_id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $data['answer']             =   I('post.answer','','htmlspecialchars');
            $data['img_src']            =   I('post.img_src','','htmlspecialchars');
            $data['sort']               =   I('post.sort','','htmlspecialchars');
            $data['confusingdata']      =   I('post.confusingdata','','htmlspecialchars');
            $data['sort']               =   I('post.sort','','htmlspecialchars');

            $result     =   M('quersion')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['quersion_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['quersion_id']))
            {
                $this->error('非法请求');
            }

            $field                  =   array('quersion_id','answer','img_src','confusingdata','sort','createtime','updatetime','appid');
            $data['game_question']  =   M('quersion')->where($where)->field($field)->find();

            if ( empty($data))
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_question_edit');
        }
    }

    public function game_question_add()
    {
        if (IS_AJAX)
        {
            $data['answer']             =   I('post.answer','','htmlspecialchars');
            $data['img_src']            =   I('post.img_src','','htmlspecialchars');
            $data['sort']               =   I('post.sort','','htmlspecialchars');
            $data['confusingdata']      =   I('post.confusingdata','','htmlspecialchars');
            $data['sort']               =   I('post.sort','','htmlspecialchars');

            $result     =   M('quersion')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_question_add');
        }
    }

    //排行榜
    public function week_ranking()
    {
        $field                  =   array('challenge_week_id','year','challenge_week','wxusers_id','checkpoint_final','createtime','exittime','passnum','appid');
        $data['week_ranking']   =   M('challenge_week')->where($where)->field($field)->select();
        $this->assign("data",$data);
        $this->display('week_ranking');
    }

    //挑战记录
    public function game_record()
    {

        $this->display('game_record');
    }
}